package com.hcycom.hcywbsocket.service;

import com.hcycom.hcywbsocket.model.Alarm;
import com.hcycom.hcywbsocket.model.InMessage;
import com.hcycom.hcywbsocket.model.OutMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Auther: CYL
 * @Date: 2018/6/7 10:20
 * @Description:
 */
@Service
public class AlarmInfoService {
    //注入发送消息模板
    @Autowired
    private SimpMessagingTemplate template;


    public void sendTopicMessage( InMessage message) throws InterruptedException{
        Alarm alarm=new Alarm();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //OutMessage outMessage=new OutMessage();
        List<Alarm> alarms=new ArrayList<Alarm>();
        for(int i=0; i<20; i++){
//            alarm.setId(i);
//            alarm.setAgentTime(sdf.format(new Date()));
//            alarm.setBccode("ceshi");
//            alarm.setCard("ceshi");
//            alarm.setCity("ceshi");
//            alarm.setDescription("ceshi");
//            alarm.setEms("ceshi");
//            alarm.setLevel("ceshi");
//            alarm.setModel("ceshi");
//            alarm.setName("ceshi");
//            alarm.setNode("ceshi");
//            alarm.setNodeType("ceshi");
//            alarm.setModel("ceshi");
//            alarm.setOrders("ceshi");
//            alarm.setOrderStatus("ceshi");
//            alarm.setFinishTime(sdf.format(new Date()));
//            alarm.setStartTime(sdf.format(new Date()));
//            alarm.setOrderTime(sdf.format(new Date()));
//            alarm.setSpecialty("ceshi");
//            alarm.setPort("ceshi");
//            alarms.add(alarm);
//            outMessage.setAlarmList(alarms);
//            Thread.sleep(2000L);         //阻塞2秒
//            template.convertAndSend(dest, outMessage.getAlarmList());
        }

    }

    /**
     *
     * 功能描述: 定时推送消息
     *
     * @param:
     * @return:
     * @auther: CYL
     * @date: 2018/6/7 11:03
     */
    public void sendAlarmInfo() {

//        int processors = Runtime.getRuntime().availableProcessors();
//
//        Long freeMem = Runtime.getRuntime().freeMemory();
//
//        Long maxMem = Runtime.getRuntime().maxMemory();
//
//        String message = String.format("服务器可用处理器:%s; 虚拟机空闲内容大小: %s; 最大内存大小: %s", processors,freeMem,maxMem );
        Alarm alarm=null;
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Alarm> alarms=new ArrayList<Alarm>();
        for(int i=0; i<200; i++){
            alarm=new Alarm();
            alarm.setId(i);
            alarm.setAgentTime(sdf.format(new Date()));
            alarm.setBccode("ceshi");
            alarm.setCard("ceshi");
            alarm.setCity("ceshi");
            alarm.setDescription("ceshi");
            alarm.setEms("ceshi");
            alarm.setLevel("ceshi");
            alarm.setModel("ceshi");
            alarm.setName("告警名称"+Math.random()*1000+1);
            alarm.setNode("ceshi");
            alarm.setNodeType("ceshi");
            alarm.setType("ceshi");
            alarm.setOrders("ceshi");
            alarm.setOrderStatus("ceshi");
            alarm.setFinishTime(sdf.format(new Date()));
            alarm.setStartTime(sdf.format(new Date()));
            alarm.setOrderTime(sdf.format(new Date()));
            alarm.setSpecialty("ceshi");
            alarm.setPort("ceshi");

            alarms.add(alarm);
        }
        template.convertAndSend("/topic/server_info",alarms);

    }
}
